package bll

import (
	"context"
	"maya-service/store/mysql"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type aiConsult struct {
	iAiConsult store.IAiConsult
}

var AiConsult = &aiConsult{
	iAiConsult: mysql.AiConsult,
}

func init() {
	Register(AiConsult)
}

func (a *aiConsult) init() func() {
	return func() {}
}

// Create
func (a *aiConsult) Create(ctx context.Context, in *model.AiConsultCreateRequest) (int64, error) {
	c := buildAiConsult(in)
	return a.iAiConsult.Create(ctx, c)
}

// Update
func (a *aiConsult) Update(ctx context.Context, in *model.AiConsultUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.UserId != nil {
		dict["user_id"] = in.UserId
	}

	if in.Level != nil {
		dict["level"] = in.Level
	}

	if in.Module != nil {
		dict["module"] = in.Module
	}

	if in.Content != nil {
		dict["content"] = in.Content
	}

	if in.ExtraContent != nil {
		dict["extra_content"] = in.ExtraContent
	}

	if in.UserFlag != nil {
		dict["user_flag"] = in.UserFlag
	}

	if in.LeaveMessage != nil {
		dict["leave_message"] = in.LeaveMessage
	}

	if in.LeaveMessageRsp != nil {
		dict["leave_message_rsp"] = in.LeaveMessageRsp
	}

	if in.LeaveMessageFlag != nil {
		dict["leave_message_flag"] = in.LeaveMessageFlag
	}

	if in.Scores != nil {
		dict["scores"] = in.Scores
	}

	if in.EvalTime != nil {
		dict["eval_time"] = in.EvalTime
	}

	if in.SupervisorId != nil {
		dict["supervisor_id"] = in.SupervisorId
	}

	if in.SupervisorName != nil {
		dict["supervisor_name"] = in.SupervisorName
	}

	if in.EvalToSupervisor != nil {
		dict["eval_to_supervisor"] = in.EvalToSupervisor
	}

	if in.Title != nil {
		dict["title"] = in.Title
	}

	if in.EvalContent != nil {
		dict["eval_content"] = in.EvalContent
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.PayStatus != nil {
		dict["pay_status"] = in.PayStatus
	}

	if in.QuestionScore != nil {
		dict["question_score"] = in.QuestionScore
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	return a.iAiConsult.Update(ctx, in, dict)
}

// Delete
func (a *aiConsult) Delete(ctx context.Context, in *model.AiConsultDeleteRequest) error {
	return a.iAiConsult.Delete(ctx, in.Id)
}

// List
func (a *aiConsult) List(ctx context.Context, in *model.AiConsultListRequest) (*model.AiConsultListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.AiConsult
		out   = &model.AiConsultListResponse{}
	)

	if total, list, err = a.iAiConsult.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.AiConsultsEntityToDto(list)

	// 用户未读数量
	if in.UserId != nil {
		flag := 0
		out.UserUnreadCount, _, _ = a.iAiConsult.List(ctx, &model.AiConsultListRequest{UserId: in.UserId, UserFlag: &flag})
	}

	// 督察师未处理数量
	if in.SupervisorId != nil && in.UserId == nil {
		out.SupervisorUnreadCount, _, _ = a.iAiConsult.List(ctx, &model.AiConsultListRequest{SupervisorId: in.SupervisorId, IsUnHandler: true})
	}

	return out, nil
}

// Find
func (a *aiConsult) Find(ctx context.Context, in *model.AiConsultInfoRequest) (*model.AiConsultInfo, error) {
	var (
		err  error
		data *entity.AiConsult
		out  = &model.AiConsultInfo{}
	)

	if data, err = a.iAiConsult.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.AiConsultEntityToDto(data)
	return out, nil
}

// buildAiConsult build entity
func buildAiConsult(in *model.AiConsultCreateRequest) *entity.AiConsult {

	ety := &entity.AiConsult{}

	ety.UserId = in.UserId

	ety.Level = in.Level

	ety.Module = in.Module

	ety.Content = in.Content

	ety.ExtraContent = in.ExtraContent

	ety.UserFlag = in.UserFlag

	ety.LeaveMessage = in.LeaveMessage

	ety.LeaveMessageRsp = in.LeaveMessageRsp

	ety.LeaveMessageFlag = in.LeaveMessageFlag

	ety.Scores = in.Scores

	ety.EvalTime = in.EvalTime

	ety.Title = in.Title

	ety.SupervisorId = in.SupervisorId

	ety.SupervisorName = in.SupervisorName

	ety.EvalContent = in.EvalContent

	ety.QuestionId = in.QuestionId

	ety.ReferenceAnswer = in.ReferenceAnswer

	ety.QuestionScore = in.QuestionScore

	ety.PayStatus = in.PayStatus

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
